// pages/addLocation/addLocation.js
var value = [0, 0, 0]; //数据位置下标
const util = require('../../request/index');
Page({

  data: {
    myRegion: ["江西省", "南昌市", "青山湖区"],
    transportValues: ["收货时间不限", "周六日/节假日收货", "周一至周五收货"],
    transportIndex: 0,
    checked: false,
    addressInfo: null,
    addressId: null
  },

  /**

   * 生命周期函数--监听页面加载

   */
  onPullDownRefresh: function () {
    this.onRefresh();
  },

  onRefresh: function () {
    //导航条加载动画
    wx.showNavigationBarLoading();
    setTimeout(function () {
      wx.hideNavigationBarLoading();
      //停止下拉刷新
      wx.stopPullDownRefresh();
    }, 2000);
  },
  onLoad: function (options) {

    // 判断对象是否为空
    if (options.addressId != undefined) {
      this.setData({
        addressId: options.addressId
      })
      this.getAddressInfo();
    }
  },
  getAddressInfo() {
    util.request_get('/web/address/info',{
      addressId: this.data.addressId
    }).then((res) => {
      console.log(res);
      if (res.code === 200){
        this.setData({
          addressInfo: res.data
        })
      }
    })
  },

  /**

   * 生命周期函数--监听页面显示

   */
  onShow: function () {

  },

  bindTransportDayChange: function (e) {
    console.log('picker country 发生选择改变，携带值为', e.detail.value);
    this.setData({
      transportIndex: e.detail.value
    })
  },
  bindRegionChange: function (e) {
    console.log(e) //查看事件返回值
    this.setData({
      myRegion: e.detail.value
    }) //重新复制给myRegion
  },

  saveAddress: function (e) {
    var consignee = e.detail.value.consignee;
    var mobile = e.detail.value.mobile;
    var transportDay = e.detail.value.transportDay;
    var detailAddress = e.detail.value.detailAddress;
    var addressRegion = this.data.myRegion[0] + " " + this.data.myRegion[1] + " " + this.data.myRegion[2] 
    if (consignee == '') {
      wx.showToast({
        title: '请输入姓名',
        image: '/icon/error.png'
      })
      return
    } else if (mobile == '') {
      wx.showToast({
        title: '请输入手机号',
        image: '/icon/error.png'
      })
      return
    } else if (detailAddress == '') {
      wx.showToast({
        title: '请输入详细地址',
        image: '/icon/error.png'
      })
      return
    } else if (mobile.length != 11) {
      wx.showToast({
        title: '手机号格式错误',
        image: '/icon/error.png'
      })
      return
    }
    var arr = wx.getStorageSync('addressList') || [];
    console.log("arr,{}", arr);
    addressInfo = {
      addressContent: consignee,
      addressPhone: mobile,
      addressRegion: addressRegion,
      detailAddress: detailAddress,
      addressUsername: transportDay,
      addressStatus: 1,
    }
    arr.push(addressInfo);
    wx.setStorageSync('addressList', arr);
    wx.navigateBack({})
    // 调用接口
  }

})